package com.wangzhenyu.learn.test.designPattern


//解释器模式


@Suppress("UNUSED")
object UInterpreterPattern {


    interface Expression {
        fun interpret(context: String): Boolean
    }

    class TerminalExpression(private val data: String) : Expression {

        override fun interpret(context: String) = context.contains(data)

    }

    class OrExpression(private val expr01: Expression, private val expr02: Expression) :
        Expression {
        override fun interpret(context: String): Boolean {
            return expr01.interpret(context) || expr02.interpret(context)
        }
    }

    class AndExpression(private val expr01: Expression, private val expr02: Expression) :
        Expression {
        override fun interpret(context: String): Boolean {
            return expr01.interpret(context) && expr02.interpret(context)
        }
    }

    //规则：Robert 和 John 是男性
    fun getMaleExpression(): Expression {
        val robert = TerminalExpression("Robert")
        val john = TerminalExpression("John")
        return OrExpression(robert, john)
    }

    //规则：Julie 是一个已婚的女性
    fun getMarriedWomanExpression(): Expression {
        val julie = TerminalExpression("Julie")
        val married = TerminalExpression("Married")
        return AndExpression(julie, married)
    }
}